package com.example.petadopt.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.text.TextUtils;

import com.example.petadopt.common.Result;
import com.example.petadopt.entity.Adopt;
import com.example.petadopt.entity.Pet;
import com.example.petadopt.utils.SqliteUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 领养数据访问对象
 */
@SuppressLint("Range")
public class AdoptDao {

    /**
     * 添加领养记录
     */
    public static Result<Void> addAdopt(Adopt adopt) {
        if (adopt == null || adopt.getPetId() == null || adopt.getAdoptUserId() == null || TextUtils.isEmpty(adopt.getAdoptTime())) {
            return new Result<>(false, "领养信息不完整", null);
        }

        // 检查宠物是否存在
        Result<Pet> petResult = PetDao.getPetById(adopt.getPetId());
        if (!petResult.isSuccess()) {
            return new Result<>(false, "宠物不存在", null);
        }

        // 检查宠物是否已经被领养
        if (isAdopted(adopt.getPetId())) {
            return new Result<>(false, "该宠物已被领养", null);
        }

        ContentValues values = new ContentValues();
        values.put("pet_id", adopt.getPetId());
        values.put("adopt_time", adopt.getAdoptTime());
        values.put("adopt_user_id", adopt.getAdoptUserId());

        long i = SqliteUtils.getInstance().getWritableDatabase().insert("tb_adopt", null, values);
        if (i > 0) {
            return new Result<>(true, "添加领养记录成功", null);
        }
        return new Result<>(false, "添加领养记录失败", null);
    }

    /**
     * 根据宠物ID查询领养记录
     */
    public static Result<Adopt> getAdoptByPetId(Long petId) {
        if (petId == null) {
            return new Result<>(false, "宠物ID不能为空", null);
        }
        Cursor cursor = SqliteUtils.getInstance().getReadableDatabase().query("tb_adopt", null, "pet_id=?", new String[]{String.valueOf(petId)}, null, null, null);
        if (cursor.moveToNext()) {
            Adopt adopt = cursorToAdopt(cursor);
            cursor.close();
            return new Result<>(true, "查询成功", adopt);
        }
        cursor.close();
        return new Result<>(false, "领养记录不存在", null);
    }

    /**
     * 根据用户ID查询领养记录
     */
    public static Result<List<Adopt>> getAdoptsByUserId(Long userId) {
        if (userId == null) {
            return new Result<>(false, "用户ID不能为空", null);
        }
        List<Adopt> adoptList = new ArrayList<>();
        Cursor cursor = SqliteUtils.getInstance().getReadableDatabase().query("tb_adopt", null, "adopt_user_id=?", new String[]{String.valueOf(userId)}, null, null, "id DESC");
        while (cursor.moveToNext()) {
            Adopt adopt = cursorToAdopt(cursor);
            adoptList.add(adopt);
        }
        cursor.close();
        return new Result<>(true, "查询成功", adoptList);
    }

    /**
     * 检查宠物是否已被领养
     */
    public static boolean isAdopted(Long petId) {
        if (petId == null) {
            return false;
        }
        Cursor cursor = SqliteUtils.getInstance().getReadableDatabase().query("tb_adopt", null, "pet_id=?", new String[]{String.valueOf(petId)}, null, null, null);
        boolean adopted = cursor.getCount() > 0;
        cursor.close();
        return adopted;
    }

    /**
     * 从Cursor提取Adopt对象
     */
    private static Adopt cursorToAdopt(Cursor cursor) {
        Adopt adopt = new Adopt();
        adopt.setId(cursor.getLong(cursor.getColumnIndex("id")));
        adopt.setPetId(cursor.getLong(cursor.getColumnIndex("pet_id")));
        adopt.setAdoptTime(cursor.getString(cursor.getColumnIndex("adopt_time")));
        adopt.setAdoptUserId(cursor.getLong(cursor.getColumnIndex("adopt_user_id")));
        return adopt;
    }
} 